/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class CheckedLabel extends JLabel {

	private static final long serialVersionUID = 7670191551407942882L;
	
	protected Icon checked_icon = getRessourceIcon("checked.png");
	protected Icon unchecked_icon = getRessourceIcon("unchecked.png");
	
	private boolean checked = false;
	
	public CheckedLabel() {
		super();
		
		setPreferredSize(new Dimension(16, 16));
		setCursor(new Cursor(Cursor.HAND_CURSOR));
		this.addMouseListener(new CheckMouseListener());
		
		setChecked(false);
	}
	
	private Icon getRessourceIcon(String name) {
		return new ImageIcon(getClass().getResource("images/" + name));
	}
	
	/**
	 * Set check state
	 * @param checked check state
	 */
	public void setChecked(boolean checked) {
		this.checked = checked;
		if (checked) {
			setIcon(checked_icon);
		} else {
			setIcon(unchecked_icon);
		}
	}
	
	/**
	 * Returns checked state
	 * @return checked state
	 */
	public boolean isChecked() {
		return checked;
	}
	
	private class CheckMouseListener implements MouseListener, Serializable {
		private static final long serialVersionUID = 8090654199537901775L;
		@Override
		public void mouseClicked(MouseEvent e) { }
		@Override
		public void mouseEntered(MouseEvent e) { }
		@Override
		public void mouseExited(MouseEvent e) { }
		@Override
		public void mousePressed(MouseEvent e) {
			if(CheckedLabel.this.isEnabled())
				setChecked(!isChecked());
		}
		@Override
		public void mouseReleased(MouseEvent e) { }
	}
}
